package member.order.action;

import m.common.action.ActionRequest;
import m.common.action.Param;
import m.common.action.RequestMethod;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import member.base.action.BaseAction;
import member.base.model.StatusModel;
import member.base.model.UserInfo;
import member.base.util.PowerUtil;
import member.order.model.MonthWages;
import member.order.model.MonthWagesDetail;
import member.order.model.OrderInfo;
import member.order.service.MonthWagesService;

import java.util.Date;

@ActionRequest(name = "action/order/monthWages",description = "月度工资")
public class MonthWagesAction extends BaseAction {
    @RequestMethod(description = "我的列表")
    public JSONMessage getMyList() throws Exception {
        ModelQueryList queryList=toQueryList(MonthWages.class,"*","confirmUser.realName");
        queryList.addCondition(QueryCondition.eq("business.oid", PowerUtil.getBusiness().getOid()));
        queryList.addCondition(QueryCondition.eq("user.oid", PowerUtil.getUser().getOid()));
        return JSONMessage.success("查询成功").push("page", ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "全部列表")
    public JSONMessage getAllList() throws Exception {
        String type=getParamsValue("type");
        ModelQueryList queryList=toQueryList(MonthWages.class,"*","user.realName","user.store.color","user.store.name","confirmUser.realName");
        if("store".equals(type)){
            queryList.addCondition(QueryCondition.eq("user.store.oid", PowerUtil.getStore().getOid()));
        }else if("business".equals(type)) {
            queryList.addCondition(QueryCondition.eq("business.oid", PowerUtil.getBusiness().getOid()));
        }
        return JSONMessage.success("查询成功").push("page", ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "获取信息")
    public JSONMessage getInfo(@Param("oid") String oid) throws Exception {
        return JSONMessage.success("查询成功")
            .push("model",ModelQueryList.getModel(MonthWages.class,oid,"*","user.realName","user.store.color","user.store.name","confirmUser.realName"));
    }
    @RequestMethod(description = "获取当前可发薪资用户")
    public JSONMessage getWagesUser() throws Exception {
        return JSONMessage.success("")
            .push("userList", ModelQueryList.getModelList(UserInfo.class,
                new String[]{"oid","status","powers","realName","phoneNum","store.name","store.color"},null,
                QueryCondition.and(
                    QueryCondition.eq("status", StatusModel.StatusEnabled),
                    QueryCondition.eq("business.oid", PowerUtil.getBusiness().getOid()),
                    QueryCondition.eq("store.oid", PowerUtil.getStore().getOid())
                )
            )
        );
    }
    @RequestMethod(description = "获取指定月份计算的工资列表")
    public JSONMessage getWagesList(@Param("yearMonth") String yearMonth) throws Exception {
        return JSONMessage.success("")
            .push("wagesList",ModelQueryList.getModelList(MonthWages.class,
                new String[]{"*","confirmUser.realName","user.oid","user.status","user.powers","user.realName","user.phoneNum","user.store.name","user.store.color"},null,
                QueryCondition.and(
                    QueryCondition.eq("yearMonth",yearMonth),
                    QueryCondition.eq("business.oid", PowerUtil.getBusiness().getOid()),
                    QueryCondition.eq("user.store.oid", PowerUtil.getStore().getOid())
                )
            )
        );
    }
    @RequestMethod(description = "计算员工薪资",transaction = true)
    public JSONMessage count(@Param("userOid") String userOid,@Param("yearMonth") String yearMonth) throws Exception {
        PowerUtil.verifyPower(UserInfo.PowerStore);
        UserInfo user=ModelQueryList.getModel(UserInfo.class,userOid,"*");
        PowerUtil.verifyStore(user.getStore());
        getService(MonthWagesService.class).count(user,yearMonth);
        return JSONMessage.success("计算成功");
    }
    @RequestMethod(description = "薪资明细")
    public JSONMessage getDetailList() throws Exception {
        ModelQueryList queryList=toQueryList(MonthWagesDetail.class,"*");
        queryList.addCondition(QueryCondition.eq("monthWages.business.oid", PowerUtil.getBusiness().getOid()));
        return JSONMessage.success("查询成功").push("page", ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "确认薪资发放")
    public JSONMessage confirm(@Param("oid") String oid) throws Exception {
        PowerUtil.verifyPower(UserInfo.PowerStore);
        MonthWages model=ModelQueryList.getModel(MonthWages.class,oid,"*","user.store.oid");
        PowerUtil.verifyStore(model.getUser().getStore());
        if(model.getPayAmount()<=0) throw new MException("非正数工资无法确认");
        model.setConfirmDate(new Date());
        model.setConfirmUser(PowerUtil.getUser());
        model.setStatus(MonthWages.Status_Done);
        ModelUpdateUtil.updateModel(model,"confirmDate","confirmUser.oid","status");
        return JSONMessage.success("确认成功");
    }
}
